import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import promiseMiddleware from 'redux-promise-middleware';
import { createLogger }  from 'redux-logger';
import rootReducer from '../reducers';

//storage
import storage from '../utils/storage'
//初始化App数据
import initAppData from '../services/initAppData'

const logger = createLogger({
  collapsed: (getState, action, logEntry) => !logEntry.error
});

let middlewares = [
    thunk,
  promiseMiddleware({
    //这里自定义ActionType后缀，默认是PENDING, FULFILLED, REJECTED
    promiseTypeSuffixes: ['LOADING', 'SUCCESS', 'ERROR']
  })
];

if (__DEV__) {
	middlewares = [...middlewares, logger]
}

export default function configureStore() {

  const finalCreateStore = compose(
    applyMiddleware(...middlewares)
  )(createStore);

  const store = finalCreateStore(rootReducer);
  //
  if (module.hot) {
    module.hot.accept(() =>{
      const nextRootReducer = require('../reducers').default
        store.replaceReducer(nextRootReducer)
      }
    );
  }
  return store;
}


